##############################################################
# Task:     Plot time-series of WVS Trust in Parliament data
#           Date: 17.02.2021
## ##############################################################

# Packages 

library(tidyverse)
library(readxl) # 1.0.0
library(reshape2)
library(ggplot2)
library(Hmisc)
library(countrycode)
library(ggrepel)





# Read in the xls file
######################

# Note: One xls sheet structured by answer categories, country and time-period

# Category 'A great deal'
#------------------------

great.deal <- as.data.frame(read_excel("WVS trust parl counts.xls", sheet = 1, range = "A4:G68", col_names = F))
names(great.deal) <- c("country", "1981-1984", "1989-1993", "1994-1998", "1999-2004", "2005-2009", "2010-2014")

great.deal <- melt(great.deal, id = "country") # Reshape
names(great.deal) <- c("country", "period", "great.deal")

# Category 'quite a lot'
#------------------------

a.lot <- as.data.frame(read_excel("WVS trust parl counts.xls", sheet = 1, range = "A74:G138", col_names = F))
names(a.lot) <- c("country", "1981-1984", "1989-1993", "1994-1998", "1999-2004", "2005-2009", "2010-2014")


a.lot <- melt(a.lot, id = "country") # Reshape
names(a.lot) <- c("country", "period", "a.lot")


# Category 'not very much'
#-------------------------

not.much <- as.data.frame(read_excel("WVS trust parl counts.xls", sheet = 1, range = "A144:G208", col_names = F))
names(not.much) <- c("country", "1981-1984", "1989-1993", "1994-1998", "1999-2004", "2005-2009", "2010-2014")

not.much <- melt(not.much, id = "country") # Reshape
names(not.much) <- c("country", "period", "not.much")


# Category 'none at all'
#-------------------------

none <- as.data.frame(read_excel("WVS trust parl counts.xls", sheet = 1, range = "A214:G278", col_names = F))
names(none) <- c("country", "1981-1984", "1989-1993", "1994-1998", "1999-2004", "2005-2009", "2010-2014")

none <- melt(none, id = "country") # Reshape
names(none) <- c("country", "period", "none")



# Merge and clean the data
##########################

parl.trust <- merge(great.deal, a.lot, by = c("country", "period"))
parl.trust <- merge(parl.trust, not.much, by = c("country", "period"))
parl.trust <- merge(parl.trust, none, by = c("country", "period"))

rm(great.deal, a.lot, not.much, none)

# Data types
parl.trust$great.deal <- as.integer(parl.trust$great.deal)
parl.trust$a.lot <- as.integer(parl.trust$a.lot)
parl.trust$not.much <- as.integer(parl.trust$not.much)
parl.trust$none <- as.integer(parl.trust$none)

# Sum of counts (= total respondents per country)
parl.trust$sum <- parl.trust$great.deal + parl.trust$a.lot + parl.trust$not.much + parl.trust$none


# Divide parl trust sum by individuell each object
parl.trust$gd.s <- parl.trust$great.deal/parl.trust$sum
parl.trust$al.s <- parl.trust$a.lot/parl.trust$sum
parl.trust$nm.s <- parl.trust$not.much/parl.trust$sum
parl.trust$n.s <- parl.trust$none/parl.trust$sum


# Calculate a net trust measure

parl.trust$net <- (parl.trust$gd.s + parl.trust$al.s) - (parl.trust$nm.s + parl.trust$n.s)


# Identify Consolidated democracies
###################################

parl.trust$ConsDem <- ifelse(grepl("Argentina|Australia|Cyprus|Germany|Japan|Netherlands|United States|Spain|Sweden|South Korea|Mexico|Poland|Turkey", parl.trust$country, fixed = F), TRUE, FALSE)
parl.trust$ConsDem <- ifelse(parl.trust$ConsDem == T, "Consolidated Democracies", "Other countries")

# Shoter country names
######################

parl.trust$isocountry <- countrycode(parl.trust$country, origin = "country.name", destination = "iso3c")

parl.trust$isocountry2 <- parl.trust$isocountry
parl.trust$isocountry2 <- ifelse(parl.trust$ConsDem == "Other countries", NA, parl.trust$isocountry2)





### Plot net trend lines for ConsDem and Other Countries 
#--- used in Appendix Item2
########################################################


# With country names only for ConsDem

ggplot(data = parl.trust, aes(x = period, y = net), group = 1)+
  # geom_jitter(width = .1)+
  # geom_text(aes(label = isocountry), position = position_jitter(width = 0.2))+
  geom_hline(yintercept = 0, linetype = "dashed")+
  stat_summary(geom = "pointrange", fun.data = "mean_cl_boot", colour = "red", label = parl.trust$country, na.rm = T, size = 1)+
  stat_summary(geom = "line", fun.data = "mean_cl_boot", colour = "red", na.rm = T, group = 1)+
  geom_point()+
  geom_text_repel(aes(label = isocountry2))+
  facet_grid(ConsDem ~ .)+
  ggtitle("WVS Trust in Parliament over time (All countries)")+
  ylab("Net trust in Parliament\n(\"A great deal\" + \"A lot\") - (\"Not so much\" + \"None at all\")\n")+
  xlab("Survey Wave")+
  theme_bw()





# Analyze Sample
#-----------------------


# Identify WVS Democracies

parl.trust$WvsDem <- ifelse(grepl("Italy|Spain|United States|Canada|Japan|Mexico|Australia|Norway|Sweden|Finland|Poland|Switzerland|Chile|Turkey|Germany|Netherlands", parl.trust$country, fixed = F), TRUE, FALSE)
parl.trust$WvsDem <- ifelse(parl.trust$WvsDem == T, "WVS Democracies", "Other Countries")




# Plotting WVS Democracies
#--- Figure 2 in Paper

# plot adding country names

ggplot(data = parl.trust[parl.trust$WvsDem == "WVS Democracies", ], aes(x = period, y = net), group = 1)+
  # geom_jitter(width = .1)+
  # geom_text(aes(label = isocountry), position = position_jitter(width = 0.2))+
  geom_point()+
  geom_text_repel(aes(label = isocountry))+
  geom_hline(yintercept = 0, linetype = "dashed")+
  stat_summary(geom = "pointrange", fun.data = "mean_cl_boot", colour = "red", label = parl.trust$country, na.rm = T)+
  stat_summary(geom = "line", fun.data = "mean_cl_boot", colour = "red", na.rm = T, group = 1)+
  ggtitle("WVS Trust in Parliament Over Time (All Countries)")+
  ylab("Net Trust in Parliament\n(\"A great deal\" + \"A lot\") - (\"Not so much\" + \"None at all\")\n")+
  xlab("Survey Wave")+
  theme_bw()

ggsave(file = "Decline of Trust in Parliaments with countrynames_Aug 2020.png", height = 20, width = 30, units = "cm")

#########################################################

